import QtQuick 2.0

Item {


    id: root
    property string title: ""

    property string button_icon: ""
    property string button_color: "white"
    property string text_color: "#333333"
    property int fontsize: 17
    property string button_border: "transparent"

    property string button_icon_select: button_icon
    property string button_color_select: button_color
    property string text_color_select: text_color
    property string button_border_select: button_border

    property string button_icon_disable: button_icon
    property string button_color_disable: "#F5F5F5"
    property string text_color_disable: "#BFBFBF"
    property string button_border_disable: "#CCCCCC"


    property real radius: 5

    property bool underlineSelect: false
    property bool isSelected: true
    property bool isDisabled: false
    property bool isPressed: false


    // 选中样式
    function setSelect(){
        isSelected = true
    }
    function setUnselect(){
        isSelected = false
    }


    width: 120
    height: 40

    signal released()
    signal released_disabled()
    Rectangle{
        id: idRectangle
        anchors.fill: parent
        color: isDisabled ? (button_color_disable) : (isSelected ? button_color_select : button_color)
        border.color: isDisabled ? (button_border_disable) : (isSelected ? button_border_select : button_border)
        radius: underlineSelect ? 0 : root.radius
    }

    // 居中显示
    Image {
        id: idImage
        height: 20
        width: 20
        anchors.centerIn: parent
        anchors.horizontalCenterOffset: -title.length*8 - 2
        source: isDisabled ? (button_icon_disable) : (isSelected ? button_icon_select : button_icon)
    }
    Text{
        id: idText
        font.pixelSize: fontsize
        anchors.centerIn: parent
        anchors.horizontalCenterOffset: button_icon==="" ? 0 : 9
        text: title
        color: isDisabled ? (text_color_disable) : (isSelected ? text_color_select : text_color)
    }

    // 按钮点击特效
    Rectangle{
        color: "#000000"
        opacity: 0.0
        visible: isPressed && (!isDisabled)
        anchors.fill: parent
    }

    MouseArea{
        anchors.fill: parent
        cursorShape: Qt.PointingHandCursor
        onPressed: {
            isPressed = true
        }
        onReleased: {
            isPressed = false
            if (!isDisabled){
                root.released()
            }else{
                root.released_disabled()
            }
            root.focus = true
        }
    }

    CustomBorder{
        id: underline
        bBorderwidth: 2
        borderColor: "#1677FF"
        visible: underlineSelect && isSelected
    }

}
